/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/05/10 by CL $
*/
$(function(){

	$(document).on("blur", "#loginname", function(){
		if ($(this).val().length >0) {
			$("#loginname_tips").hide();
		}
	});
	$(document).on("blur", "#password", function(){
		if ($(this).val().length >0) {
			$("#password_tips").hide();
		}
	});
	$(document).on("blur", "#checkcode", function(){
		if ($(this).val().length >0) {
			$("#checkcode_tips").hide();
		}
	});


	//提交登录
	$(document).on("click", "[f='but_loginpost']", function(){
		$loginname = $("#loginname").val();
		$password = $("#password").val();
		$loading = $(this).attr("data-loading");
		if (typeof($loading) == "undefined") {
			$loading = "";
		}
		if ($loginname.length == 0) {
			$("#loginname_tips").show();
			return false;
		}
		
		if ($password.length == 0) {
			$("#password_tips").show();
			return false;
		}

		$checkcode = $("#checkcode").val();
		if (typeof($checkcode) == "undefined") {
			$checkcode = "";
		}
		else {
			if ($checkcode.length == 0) {
				$("#checkcode_tips").show();
				return false;
			}
		}
		if ($loading.length > 0) {
			$("#"+$loading).show();
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport",
			cache: false,
			data: {a:"loginpost", loginname:$loginname, password:$password, checkcode:$checkcode, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") { //成功，返回页面
					$forward = "";
					if ($forward.length == 0) {
						$forward = _ROOT_PATH+"index.php?m=user";
					}
					window.location.href = $forward;					
				}
				else if ($response == "2") { //整合OEmarry
					window.top.location.href = $result;
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("登录失败，请检查帐号密码是否正确");
					}
					if ($loading.length > 0) {
						$("#"+$loading).hide();
					}
					$("#img_checkcode").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=65&height=30&num=5&r="+get_rndnum(8));
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});

	//刷新
	$(document).on("click", "[f='but_refresh']", function(){
		$("#img_checkcode").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=65&height=30&num=5&r="+get_rndnum(8));
	});

});